package com.bbs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


public class ColorDaoOfHome {
	static Logger logger = Logger.getLogger(ColorDaoOfHome.class.getName());
	public List<String> colorList = new ArrayList<>();
	private Connection conn ;
	public boolean insertColor(String color) {
		boolean isInsert = false;
		PreparedStatement ps = null;
		String sql = "INSERT INTO core_dict(dict_word,dict_type) VALUES (?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, color);
			ps.setInt(2, 1);
			isInsert = ps.executeUpdate() == 1 ? true : false;
			logger.info(color + " 插入成功");
		} catch (SQLException e) {
			logger.error(color + " 插入失败");
		}
		return isInsert;
	}

	public List<String> getColor() {
		List<String> colorList = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT dict_word FROM core_dict";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				colorList.add(rs.getString("dict_word"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return colorList;
	}

	public void isInsert(String s) {
		boolean isinsert = colorList.contains(s);
		if (!isinsert) {
			colorList.add(s);
			insertColor(s);
		}

	}

	public ColorDaoOfHome(Connection conn) {
		super();
		this.conn = conn;
	}
	
	

}
